package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeEdu;
import com.ruoyi.food.mapper.OpmFoodSafeEduMapper;
import com.ruoyi.food.service.IOpmFoodSafeEduService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品安全教育Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeEduServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeEduService {
    @Autowired
    private OpmFoodSafeEduMapper opmFoodSafeEduMapper;

    /**
     * 查询食品安全教育
     *
     * @param id 食品安全教育ID
     * @return 食品安全教育
     */
    @Override
    public OpmFoodSafeEdu selectOpmFoodSafeEduById(Long id) {
        OpmFoodSafeEdu result = opmFoodSafeEduMapper.selectOpmFoodSafeEduById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询食品安全教育列表
     *
     * @param opmFoodSafeEdu 食品安全教育
     * @return 食品安全教育
     */
    @Override
    public List<OpmFoodSafeEdu> selectOpmFoodSafeEduList(OpmFoodSafeEdu opmFoodSafeEdu) {
        return opmFoodSafeEduMapper.selectOpmFoodSafeEduList(opmFoodSafeEdu);
    }

    /**
     * 新增食品安全教育
     *
     * @param opmFoodSafeEdu 食品安全教育
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeEdu(OpmFoodSafeEdu opmFoodSafeEdu) {
        opmFoodSafeEdu.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeEdu.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeEdu.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeEdu.getFileIdList(), opmFoodSafeEdu.getId().toString());
        return opmFoodSafeEduMapper.insertOpmFoodSafeEdu(opmFoodSafeEdu);
    }

    /**
     * 修改食品安全教育
     *
     * @param opmFoodSafeEdu 食品安全教育
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeEdu(OpmFoodSafeEdu opmFoodSafeEdu) {
        opmFoodSafeEdu.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeEdu.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeEdu.getFileIdList(), opmFoodSafeEdu.getId().toString());
        return opmFoodSafeEduMapper.updateOpmFoodSafeEdu(opmFoodSafeEdu);
    }

    /**
     * 批量删除食品安全教育
     *
     * @param ids 需要删除的食品安全教育ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeEduByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeEduMapper.deleteOpmFoodSafeEduByIds(ids);
    }

    /**
     * 删除食品安全教育信息
     *
     * @param id 食品安全教育ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeEduById(Long id) {
        return opmFoodSafeEduMapper.deleteOpmFoodSafeEduById(id);
    }
}
